package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
)
func main() {
	db, err := sql.Open(
		"mysql",
		"root:root@tcp(172.20.0.10:3306)/gogogo?charset=utf8",
	)
	if err != nil {
		log.Fatal("open db error.", err)
		return
	}
	defer db.Close()

	stmt, err := db.Prepare("select * from tbl_student where id = ?")
	if err != nil {
		log.Fatal("prepare error.", err)
	}
	defer stmt.Close()
	//查询数据
	rows, err := stmt.Query(1)
	if err != nil {
		log.Fatal("query error.", err)
	}
	var (
		id int
		stuNo string
		name string
	)
	for rows.Next() {
		err := rows.Scan(&id, &stuNo, &name)
		if err != nil {
			log.Fatal("scan error.", err)
		}
		fmt.Printf("id=%d, stuNo=%s, name=%s\n", id, stuNo, name)
	}

}
